package com.tuanzi.loan.business.service.businesssystem.impl;

import com.tuanzi.loan.business.entity.businesssystem.BusinessBill;
import com.tuanzi.loan.business.entity.businesssystem.BusinessMain;
import com.tuanzi.loan.business.repository.businesssystem.BusinessBillRepository;
import com.tuanzi.loan.business.repository.businesssystem.BusinessMainRepository;
import com.tuanzi.loan.business.service.businesssystem.BusinessBillService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.businesssystem.BusinessBillRequest;
import com.tuanzi.loan.vo.response.businesssystem.BusinessBillResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.locks.ReentrantLock;


@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class BusinessBillServiceImpl extends BaseAuditServiceImpl<BusinessBill, Long> implements BusinessBillService {
    @Autowired
    private BusinessBillRepository settlementBillRepository;

    @Autowired
    private BusinessMainRepository settlementCompanyRepository;

    @Override
    public Page<BusinessBillResponse> list(PageSearcher<BusinessBillRequest> pageSearcher) {
        return settlementBillRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
    }

    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void onSave(BusinessBill settlementBill) {
        //保存开票记录
        Long settlementPrepayId = settlementBill.getId();

        //更新公司的预付金额

        BusinessMain company = null;
        BusinessBill oldBill =null;
        Double settlementPrepayAmount = null;//充值金额
        Double companyPrepayAmount = null;
        try {
            lock.lock();//保证只有一个线程在执行更新操作
            company = settlementCompanyRepository.findOne(settlementBill.getCompanyId());
            companyPrepayAmount = company.getBillAmount();
            settlementPrepayAmount = settlementBill.getBillAmount();
            if (companyPrepayAmount == null) {
                company.setBillAmount(settlementPrepayAmount);
            } else {
                if (settlementPrepayId == null) { //如果是添加预付记录，公司的预付金额 = 原始预付金额+充值金额
                    company.setBillAmount(companyPrepayAmount + settlementPrepayAmount);
                } else {//如果是修改预付记录 ， 公司的预付金额 = 原始预付金额 - 上一次开票金额 + 要更新的开票金额
                    oldBill = settlementBillRepository.findOne(settlementPrepayId);
                    company.setBillAmount(companyPrepayAmount-oldBill.getBillAmount()+settlementPrepayAmount);
                }
            }
            settlementCompanyRepository.save(company);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

        //保存预付记录
        settlementBillRepository.save(settlementBill);
    }

    @Override
    public Double getAllIncome(BusinessBillRequest $) {
        return settlementBillRepository.getAllIncome($);
    }

    @Override
    protected JPAAuditRepository<BusinessBill, Long> getRepository() {
        return settlementBillRepository;
    }
}
